package com.caojm.mptools.client.commands;

import com.caojm.mptools.client.PermissionManager;
import com.caojm.mptools.client.utils.LoggerUtil;  // 引入日志工具类
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import com.mojang.brigadier.arguments.StringArgumentType;

public class BindCommand {

    private static PermissionManager permissionManager = new PermissionManager();

    public static void register() {
        CommandManager.literal("mp")
                .then(CommandManager.literal("bind")  // 绑定命令
                        .then(CommandManager.argument("account", StringArgumentType.string())  // 获取账号参数
                                .executes(context -> {
                                    ServerCommandSource source = context.getSource();
                                    String playerName = source.getName();

                                    // 检查玩家是否有权限执行绑定操作
                                    if (!permissionManager.hasPermission(playerName, "mingpixeltools.bind")) {
                                        source.sendError(Text.of("你没有权限执行此操作！"));
                                        LoggerUtil.logWarning("Player " + playerName + " attempted to use /mp bind without proper permission.");
                                        return 0;
                                    }

                                    String account = StringArgumentType.getString(context, "account");

                                    // 执行账号绑定的逻辑（假设绑定成功）
                                    source.sendFeedback(() -> Text.of("账号绑定成功: " + account), false);
                                    LoggerUtil.logInfo("Player " + playerName + " successfully bound their account to: " + account);
                                    return 1;  // 返回成功
                                })
                        )
                )
                .build();
    }
}
